package com.hjl.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * 文件下载工具包
 *
 * @author Lenovo
 * @version 1.0
 * @date 2021/10/8 20:24
 */
public class DownLoadUtils {
    private static final String BROWSER1 =  "MSIE";
    private static final String BROWSER2 =  "Firefox";
    public static String getFileName(String agent, String filename) throws UnsupportedEncodingException {

        if (agent.contains(BROWSER1)) {
            // IE浏览器
            filename = URLEncoder.encode(filename, "utf-8");
            filename = filename.replace("+", " ");
        } else if (agent.contains(BROWSER2)) {
            // 火狐浏览器
//            BASE64Encoder base64Encoder = new BASE64Encoder();
//            filename = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?=";
            Base64.Encoder encoder = Base64.getEncoder();
            filename = "=?utf-8?B?" + encoder.encodeToString(filename.getBytes(StandardCharsets.UTF_8)) + "?=";
        } else {
            // 其它浏览器
            filename = URLEncoder.encode(filename, "utf-8");
        }
        return filename;
    }
}
